//
//  UserListViewController.m
//  豆瓣
//
//  Created by dllo on 15/10/27.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "UserListViewController.h"
#import "LoginViewController.h"
#import "CollectionViewController.h"
#import "DataBaseSingleton.h"

@interface UserListViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain)UITableView *myTableView;

@end

@implementation UserListViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:@"islogin"];
    if ([name isEqualToString:@"1"]) {
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"注销" style:UIBarButtonItemStylePlain target:self action:@selector(Action:)];
    }else{
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"登陆" style:UIBarButtonItemStylePlain target:self action:@selector(buttonAction:)];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
    self.myTableView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myTableView];
    [self.myTableView release];
    
    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    self.navigationItem.title = @"我的";
    
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"登陆" style:UIBarButtonItemStylePlain target:self action:@selector(buttonAction:)];
    
}

- (void)Action:(UIBarButtonItem *)bbb
{
    UIAlertController *alertCon = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否确定注销" preferredStyle:UIAlertControllerStyleAlert];
    
    [self presentViewController:alertCon animated:YES completion:^{
        
    }];
    UIAlertAction *alertActCn = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        
        [[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"islogin"];
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"登陆" style:UIBarButtonItemStylePlain target:self action:@selector(buttonAction:)];
        
    }];
    [alertCon addAction:alertActCn];
    UIAlertAction *alertActCn2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        
    }];
    [alertCon addAction:alertActCn2];
    
}

- (void)buttonAction:(UIBarButtonItem *)btn
{
    LoginViewController *login = [[LoginViewController alloc] init];
    UINavigationController *loginVC = [[UINavigationController alloc] initWithRootViewController:login];
    [loginVC setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    [self presentViewController:loginVC animated:YES completion:^{
        
        
    }];
    [login release];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if (indexPath.row == 0) {
        cell.textLabel.text = @"我的活动";
    }else if (indexPath.row == 1){
        cell.textLabel.text = @"我的收藏";
    }else{
        cell.textLabel.text = @"清除缓存";
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:@"islogin"];
    if ([name isEqualToString:@"0"]) {
    
    LoginViewController *login = [[LoginViewController alloc] init];
    UINavigationController *loginVC = [[UINavigationController alloc] initWithRootViewController:login];
    [loginVC setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
    [self presentViewController:loginVC animated:YES completion:^{
        
        
    }];
    [login release];
        
    }
    else if (indexPath.row >= 0 && indexPath.row < 2){
        
        CollectionViewController *collectionVC = [[CollectionViewController alloc] init];
        [self.navigationController pushViewController:collectionVC animated:YES];
        [collectionVC release];
        
        NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:@"name"];
        if (indexPath.row == 0) {
            collectionVC.title = @"我的活动";
            collectionVC.listArray = [[DataBaseSingleton shareDataBase] selectMovieName:name];
        }else{
            collectionVC.title = @"我的收藏";
        }
    }
    else{
        
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
